# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class JdReptilePipeline:

    # def __init__(self):
    #     pass
        # 获取setting中主机名，端口号和集合名
        # host = settings['MONGODB_HOST']
        # port = settings['MONGODB_PORT']
        # dbname = settings['MONGODB_DBNAME']
        # col = settings['MONGODB_COL']
        #
        # # 创建一个mongo实例
        # client = MongoClient(host=host,port=port)
        #
        # # 访问数据库
        # db = client[dbname]
        #
        # # 访问集合
        # self.col = db[col]

    def open_spider(self, spider):
        self.fp = open('book.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        print("=================================================================")
        self.fp.write(str(item))
        print("=================================================================")
        return item

    def close_spider(self, spider):
        self.fp.close()


    # def process_item(self, item, spider):
    #     data = dict(item)
    #     print(str(data))
    #     return item
